package iot.jtt808.platform;

import iot.jtt808.Decoder;
import iot.jtt808.Encoder;
import iot.utils.DataPackUtil;

/**
 * Created by dave on 18-7-20 下午3:12.
 * 终端注册应答，0x8100
 */
public class TerminalRegisterResp implements Encoder, Decoder<TerminalRegisterResp> {
    private int terminalSequenceNo; // 终端消息流水号
    private int result;             // 注册结果，0:成功;1: 车辆已被注册;2: 数据库中无该车辆;3: 终端已被注册; 4:数据库中无该终端
    private String auth;            // 鉴权码

    @Override
    public byte[] encode() {
        byte[] ab = auth.getBytes();
        byte[] ret = new byte[3 + ab.length];
        DataPackUtil.writeWord(ret, 0, terminalSequenceNo);
        DataPackUtil.writeByte(ret, 2, result);
        DataPackUtil.writeBytes(ret, 3, ab);
        return ret;
    }

    @Override
    public TerminalRegisterResp decode(byte[] bytes, int start, int end) {
        terminalSequenceNo = DataPackUtil.readWord(bytes, start);
        result = bytes[start + 2];
        auth = DataPackUtil.bytesToString(bytes, start + 3, end);
        return this;
    }

    public int getTerminalSequenceNo() {
        return terminalSequenceNo;
    }

    public void setTerminalSequenceNo(int terminalSequenceNo) {
        this.terminalSequenceNo = terminalSequenceNo;
    }

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

    public String getAuth() {
        return auth;
    }

    public void setAuth(String auth) {
        this.auth = auth;
    }
}
